package edu.yuhf.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import edu.yuhf.domain.Hobby;
import edu.yuhf.domain.NativePlace;
import edu.yuhf.domain.User;
import edu.yuhf.domain.UserDetail;
import edu.yuhf.service.HobbyServiceImpl;
import edu.yuhf.service.NativePlaceServiceImpl;
import edu.yuhf.service.UserServiceImpl;
import edu.yuhf.service.iface.HobbyService;
import edu.yuhf.service.iface.NativePlaceService;
import edu.yuhf.service.iface.UserService;


@WebServlet("/user.servlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger log=Logger.getLogger(UserServlet.class);
	
	private UserService userService=new UserServiceImpl();
	private HobbyService hobbyService=new HobbyServiceImpl();
	private NativePlaceService nativePlaceService=new NativePlaceServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out=response.getWriter();
		
		String param=request.getParameter("param");
		
		String ids=request.getParameter("ids");
		String name=request.getParameter("userName");
		String password=request.getParameter("password");
		String sex=request.getParameter("sex");
		String email=request.getParameter("email");
		String cityCode=request.getParameter("cityCode");
		String[] favs=request.getParameterValues("fav");
		
		String isCookie=null==request.getParameter("isCookie")?"no":request.getParameter("isCookie");
		
		if(Objects.equals("init", param)) {
			List<Map<String,Object>> users=userService.showAll();
			hobbyService.setHobbyName(users);
			request.setAttribute("users",users);
			request.getRequestDispatcher("views/userManager.jsp").forward(request, response);
		}
		if(Objects.equals("delete", param)) {
			if(userService.removeUsers(ids)) {
				out.print("<script>alert('删除成功！');window.location.href='user.servlet?param=init'</script>");
			}else {
				out.print("<script>alert('删除失败！');window.location.href='user.servlet?param=init'</script>");
			}
		}
		if(Objects.equals("doRegister", param)) {
			User user=new User(0,name,password,sex,email);
			UserDetail ud=new UserDetail(0,cityCode,String.join(",", favs),0);
			if(userService.register(user, ud)) {
				out.print("<script>alert('注册成功，请在登录页面登录。');window.location.href='init.servlet'</script>");
			}else {
				out.print("<script>alert('注册失败，请换个时间再试一次。');window.location.href='init.servlet'</script>");
			}
		}
		if(Objects.equals("register", param)) {
			List<Hobby> hobbyList=hobbyService.getHobbies();
			List<NativePlace> provinceList=nativePlaceService.getProvince();
			request.setAttribute("hobbies", hobbyList);
			request.setAttribute("provinces", provinceList);
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		if(Objects.equals("login",param)) {
			if(userService.login(name, password)) {
				//cookie操作
				if(Objects.equals(isCookie, "yes")) {
					Cookie cookie=new Cookie("web","www.nnblog.com");
					cookie.setMaxAge(60*60*24);
					response.addCookie(cookie);
				}
				HttpSession session=request.getSession();
				session.setAttribute("name", name);
				request.getRequestDispatcher("views/main.jsp").forward(request, response);
			}else {
				request.setAttribute("msg","用户名或密码错误!!!");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		}
		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
